package com.skyline.feed.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mongodb.DBObject;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.feed.model.FeedResult;
import com.skyline.feed.service.PersonalFeedService;
import com.skyline.feed.service.SubscribedFeedService;
import com.skyline.user.model.Fan;
import com.skyline.user.model.User;

/**
 * Feed的Controller，所有操作都是只针对于AJAX应用的
 * 
 * @author Jairus Chan
 * 
 */
@Controller("feedController")
@RequestMapping("/feed")
public class FeedController {
	@Autowired
	private SubscribedFeedService subscribedFeedService;
	@Autowired
	private PersonalFeedService personalFeedService;

	@RequestMapping(value = "/list/{ownerId}", method = RequestMethod.GET)
	public ModelAndView getFeeds(@PathVariable Long ownerId) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName(ViewPaths.FEED_PERSONALFEED);
		mav.addObject("ownerId", ownerId);
		return mav;
	}

	/**
	 * 获取指定用户的Feed
	 * 
	 * @param ownerId
	 *            用户的Id
	 * @param current
	 *            当前的记录
	 * @param level
	 *            当前的等级
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody
	FeedResult getFeeds(Long ownerId, Integer current, Integer level) {
		return personalFeedService.getFeeds(ownerId, current, level);
	}

	/**
	 * 获取自己的feed
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param current
	 *            当前记录
	 * @param level
	 *            当前等级
	 * @return
	 */
	@RequestMapping(value = "/listMyFeeds", method = RequestMethod.POST)
	@SuppressWarnings("unchecked")
	public @ResponseBody
	FeedResult getMyFeeds(HttpServletRequest request, Integer current, Integer level) {
		User user = (User) WebHelper.getSessionAttribute(request, Constant.SESSION_USER);
		List<Fan> fans = (List<Fan>) WebHelper.getSessionAttribute(request, Constant.SESSION_IDOLS);
		// 用户已经登陆
		if (user != null && user.getId() != 0 && fans != null) {
			List<Long > idolIds=new ArrayList<Long>();
			for(Fan fan : fans){
				idolIds.add(fan.getIdolId());
				//System.out.println(fan.getIdolId());
			}
			//long t1=System.currentTimeMillis();
			FeedResult result = subscribedFeedService.getFeeds(idolIds, user.getId(), current, level);
			if (current == 0 && level == 0) {
				WebHelper.setSessionAttribute(request, Constant.SESSION_LAST_QUERY_TIME, (new Date()).getTime());
			}
			//long t2=System.currentTimeMillis();
			//System.out.println(t2-t1);
			return result;
		}
		// 用户没有登陆
		else {
			return null;
		}
	}

	/**
	 * 获取自己的最新的feed
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getNewFeeds", method = RequestMethod.POST)
	@SuppressWarnings("unchecked")
	public @ResponseBody
	List<DBObject> getNewFeeds(HttpServletRequest request) {
		User user = (User) WebHelper.getSessionAttribute(request, Constant.SESSION_USER);
		List<Fan> fans = (List<Fan>) WebHelper.getSessionAttribute(request, Constant.SESSION_IDOLS);
		// 用户已经登陆
		if (user != null && user.getId() != 0 && fans != null) {
			List<Long > idolIds=new ArrayList<Long>();
			for(Fan fan : fans){
				idolIds.add(fan.getIdolId());
				//System.out.println(fan.getIdolId());
			}
			Long lastQueryTime = (Long) WebHelper.getSessionAttribute(request, Constant.SESSION_LAST_QUERY_TIME);
			WebHelper.setSessionAttribute(request, Constant.SESSION_LAST_QUERY_TIME, (new Date()).getTime());
			//System.out.println(lastQueryTime);
			return subscribedFeedService.getNewFeeds(idolIds, user.getId(), lastQueryTime);
		}
		// 用户没有登陆
		//1315899033276
		//1320173733432
		//1320147706910
		//1320147586919
		//
		else {
			return null;
		}
	}
}
